package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.refundStorage.RefundStorageSaleOperatorListAdapter;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorGroupBean;
import com.inossem.warehousestorage_inossem.bean.common.GetRoleBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageSaleOverviewBean;
import com.inossem.warehousestorage_inossem.constant.TypeEnum;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.common.GetOperatorGroupRequest;
import com.inossem.warehousestorage_inossem.request.common.GetRoleRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.RefundStorageUtils;

import java.util.List;

public class RefundStorageSaleAddOperatorActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullableListView operatorList;

    private RefundStorageSaleOperatorListAdapter adapter;

    private List<GetOperatorGroupBean.OperatorGroupBean> groupListBeanList;

    private List<GetRoleBean.RoleBean> mRoleList;

    private String mRoleName, mRoleCode, mGroupName, mGroupCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_picking_add_operator);
        initView(RefundStorageSaleAddOperatorActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(RefundStorageSaleAddOperatorActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        operatorList = (PullableListView) activity.findViewById(R.id.operatorList);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_refund_storage_picking_add_operator_withdrawing_from_the_library));
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (RefundStorageUtils.saleHasOperator(activity)) {
                    Intent intent = new Intent(activity, RefundStorageSaleConfirmActivity.class);
                    startActivity(intent);
                } else {
                    Utils.middleToast(activity, activity.getResources().getString(R.string.activity_receive_add_receiver_fill_in_receiver));
                }
            }
        });
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_up_frame_commit_back), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        operatorList.setPullUpFlag(false);
        operatorList.setPullDownFlag(false);
        getData(activity);
    }

    private void getData(Context context) {
        if (mRoleList == null) {
            GetRoleRequest.request(context, TypeEnum.REFUND_STORAGE_SALE, new GsonBeanListener<GetRoleBean>() {
                @Override
                public void onSuccess(GetRoleBean bean) {
                    mRoleList = bean.getBody();
                    RefundStorageSaleOverviewBean.SaleBodyBean data = RefundStorageUtils.getRefundStorageSaleOverviewBean(context).getBody();
                    addReceiverListData(context, data, operatorList);
                }
            });
        } else {
            RefundStorageSaleOverviewBean.SaleBodyBean data = RefundStorageUtils.getRefundStorageSaleOverviewBean(context).getBody();
            addReceiverListData(context, data, operatorList);
        }
    }

    private void addReceiverListData(Context context, RefundStorageSaleOverviewBean.SaleBodyBean data, ListView listView) {
        adapter = new RefundStorageSaleOperatorListAdapter(context, data, mRoleList, mRoleName, mRoleCode, mGroupName, mGroupCode, new RefundStorageAddOperatorListener() {
            @Override
            public void onDeleteOperatorListener() {
                initData(RefundStorageSaleAddOperatorActivity.this);
            }

            @Override
            public void onSelectRole(String roleName, String roleCode) {
                mRoleName = roleName;
                mRoleCode = roleCode;
            }

            @Override
            public void onClickGroupNameListener() {
                getUserGroup(RefundStorageSaleAddOperatorActivity.this);
            }
        });
        listView.setAdapter(adapter);
    }

    private void getUserGroup(Context context) {
        if (groupListBeanList == null) {
            GetOperatorGroupRequest.request(context, TypeEnum.REFUND_STORAGE_SALE, new GsonBeanListener<GetOperatorGroupBean>() {
                @Override
                public void onSuccess(GetOperatorGroupBean bean) {
                    groupListBeanList = bean.getBody();
                    showGroupDialog(context);
                }
            });
        } else {
            showGroupDialog(context);
        }

    }

    private void showGroupDialog(Context context) {
        DialogUtils.getRefundStorageUserGroupDialog(context, groupListBeanList, new DialogUtils.RoleListener() {
            @Override
            public void onSelect(Dialog dialog, String groupNameStr, String groupCodeStr) {
                RefundStorageUtils.addGroupSaleAddOperator(context, RefundStorageUtils.getSaleAddOperatorListByGroupList(context, groupListBeanList, groupCodeStr));
                mGroupName = groupNameStr;
                mGroupCode = groupCodeStr;
                initData(RefundStorageSaleAddOperatorActivity.this);
            }
        }).show();
    }

    public interface RefundStorageAddOperatorListener {
        void onDeleteOperatorListener();

        void onSelectRole(String roleName, String roleCode);

        void onClickGroupNameListener();
    }

}
